#include "AnaDef.h"
#include "AnaAliDef.h"
#include "AliTRDdEdxBaseUtils.h"

#include "IniRun.h"

void rec(const TString filename, const Int_t event0, const Int_t event1, const Int_t kgrid)
{
  printf("filename %s, event %d %d\n", filename.Data(), event0, event1);

  IniRun(filename, 0x0, kgrid, kTRUE);
  
  const TString libs[]={"ITSbase", "HLTbase", "EMCALUtils", "EMCALraw", "EMCALbase", "EMCALrec", "EMCALsim", "TOFbase", "TOFsim", "RAWDatasim"};
  const Int_t nlib = sizeof(libs)/sizeof(TString);
  LoadLibs(nlib, libs);

  //==================================

  // Reconstruction settings
  AliReconstruction *rec=new AliReconstruction;

  AliTRDdEdxBaseUtils::SetPadGainOn(kTRUE);

  // All friends
  //this works
  rec->SetFractionFriends(10000);

  // Set protection against too many events in a chunk (should not happen)
  rec->SetEventRange(event0, event1);

  //
  // QA options
  //
  rec->SetRunQA(":");
  rec->SetRunGlobalQA(kFALSE);

  // AliReconstruction settings
  rec->SetWriteESDfriend(kTRUE);
  rec->SetWriteAlignmentData();
  rec->SetInput(filename);
  //rec->SetUseTrackingErrorsForAlignment("ITS");
  //rec->SetRunReconstruction("ALL");
  rec->SetRunReconstruction("TPC TRD");
  //rec->SetFillESD("ALL");
  rec->SetFillESD("TPC TRD");

  //rec->SetRunReconstruction("ALL");
  //rec->SetRunQA("Global:ESDs") ;
  //rec->SetRunQA(":") ;
  //rec->SetRunQA("ALL -MUON:ALL") ;
  //rec->SetRunQA("ALL:ALL") ;
  //rec->SetQARefDefaultStorage("local://$ALICE_ROOT/QAref") ;
  //rec->SetWriteESDfriend(kTRUE);
  //rec->SetWriteAlignmentData();
  //rec->SetInput(filename);
  //rec->SetUseTrackingErrorsForAlignment("ITS");

  // switch off cleanESD
  rec->SetCleanESD(kFALSE);

  //Ignore SetStopOnError
  rec->SetStopOnError(kFALSE);

  AliLog::Flush();
  rec->Run();

  delete rec;

  printf("exiting...\n");
}

int main(int argc, char *argv[])
{
  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n",ii, argv[ii]);
  }

  if(argc!=5){
    printf("argc!=5  !");
  }

  rec(argv[1],atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
}
